<template>
  <div class="search-result">
    <!-- v-model 控制是否在加载中 -->
    <!-- finished控制是否结束 -->
    <!-- load事件，加载数据逻辑 页面打开就触发，直到页面铺满停止；滚动到底部继续触发 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import { getSearchResultApi } from "@/api/Search";
export default {
  name: "SearchResult",
  props: {
    // 输入框文字
    searchText: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1, // 页码
      per_page: 10, // 每页数量
    };
  },
  methods: {
    async onLoad() {
      try {
        // 1. 加载数据
        let { data } = await getSearchResultApi({
          page: this.page,
          per_page: this.per_page,
          q: this.searchText, // 输入框文字
        });
        // 2. 追加数据到列表
        this.list.push(...data.data.results);
        // 3. 关闭加载中
        this.loading = false;
        // 4. 判断是否结束
        if (data.data.results.length < this.per_page) {
          // ===>4.1 已结束 finished变true
          this.finished = true;
        } else {
          // ===>4.2 未结束 页码+1
          this.page++;
        }
      } catch (error) {
        this.loading = false;
        console.log(error);
      }
    },
  },
};
</script>

<style scoped lang="less"></style>
